﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Users : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      // For admin pages, check the current user is an admin person
      if (!Conn.IsAdminUser())
      {
        Response.Redirect("Default.aspx");
      }

      if (!IsPostBack)
      {
        using (SqlConnection conn = Conn.GetOpened())
        {
          string sql = "select email,admin from users";
          SqlCommand cmd = new SqlCommand(sql, conn);
          SqlDataReader rdr = cmd.ExecuteReader();
          while (rdr.Read())
          {
            SqlString email = rdr.GetSqlString(0);
            SqlBoolean admin = rdr.GetSqlBoolean(1);
            bool isAdmin = !admin.IsNull && admin.Value;
            litUsers.Text += string.Format(
              @"
<a href='Admin-Users-Delete.aspx?email={3}' title='Delete'><img src='Assets/icons/delete.png'/></a>
<img src='Assets/icons/{0}.png'/> {1} {2}<br />",
              isAdmin ? "user_gray" : "user",
              email,
              isAdmin ? "(administrator)" : "",
              HttpUtility.UrlEncode(email.ToString())
              );
          }
        }
      }
    }
    protected void bnAddNew_Click(object sender, EventArgs e)
    {
      Response.Redirect("Admin-Users-New.aspx");
    }
}
